﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMAS.DAL;
using HLVControl.Grid;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace RMAS
{
    public partial class usrc_ListWareHouse : UserControl
    {
        bool KQ = false;
        DM_WAREHOUSE_Provider _prov;
        public usrc_ListWareHouse()
        {
            InitializeComponent();
        }
        private void usrc_ListWareHouse_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        public void LoadAll()
        {
            _prov = new DM_WAREHOUSE_Provider();
            LoadToTreeview(treeview, StaticVar.DanhSachKhoSP);
            UIHelper.LoadFocus(txtTimKiem);
        }

        private void LoadToTreeview(TreeListView treeview, DM_WAREHOUSEList list)
        {
            try
            {

                treeview.Rows.Clear();
                int count = 1;
                if (list == null) return;
                foreach (DM_WAREHOUSE obj in list)
                {
                    TreeListRow row = treeview.CreateRow();
                    row.Cells.Add(new TreeListCell(count));
                    row.Cells.Add(new TreeListCell(obj.WHID));
                    row.Cells.Add(new TreeListCell(obj.WHName));
                    row.Cells.Add(new TreeListCell(obj.WHKeeper));
                    row.Cells.Add(new TreeListCell(obj.WHAdress));
                    row.Cells.Add(new TreeListCell(obj.WHPhone));
                    row.Cells.Add(new TreeListCell(obj.WHNotes));
                 
                    treeview.Rows.Add(row);
                    count++;
                }
                treeview.Refresh();
            }
            catch { }
        }
        private void XuLy(EnumGUI.ThaoTac thaotac)
        {
            try
            {
                DM_WAREHOUSE obj = null;
                if (treeview.SelectedElements.Count != 0)
                {
                    TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                    string ID = firstSelected.Cells["colID"].Value.ToString();
                    obj = _prov.GetBy_WHID(ID);
                }


                switch (thaotac)
                {
                    case EnumGUI.ThaoTac.them:
                        frmDM_WareHouse fAdd = new frmDM_WareHouse(null, EnumGUI.ThaoTac.them);
                        if (fAdd.ShowDialog() == DialogResult.OK)
                        {
                            KQ = true;
                        }
                        break;
                    case EnumGUI.ThaoTac.sua:
                        if (obj != null)
                        {
                            frmDM_WareHouse fEdit = new frmDM_WareHouse(obj, EnumGUI.ThaoTac.sua);
                            if (fEdit.ShowDialog() == DialogResult.OK)
                            {
                                KQ = true;
                            }
                        }
                        else MessageBox.Show("Vui lòng chọn khu vực trong danh sách", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    case EnumGUI.ThaoTac.xoa:
                        if (obj != null)
                        {
                            if (MessageBox.Show("Bạn có muốn xóa khu vực này không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                KQ = _prov.Cmd_XoaDM_WAREHOUSE(obj);
                                MessageBox.Show(_prov.ErrMsg.Replace("DM_WAREHOUSE", "khu vực"), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else MessageBox.Show("Vui lòng chọn khu vực trong danh sách", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    case EnumGUI.ThaoTac.xem:
                        if (obj != null)
                        {
                            frmDM_WareHouse fView = new frmDM_WareHouse(obj, EnumGUI.ThaoTac.xem);
                            if (fView.ShowDialog() == DialogResult.OK)
                            {
                                KQ = true;
                            }
                        }

                        break;
                    case EnumGUI.ThaoTac.nhapexcel:

                        break;
                    case EnumGUI.ThaoTac.xuatexcel:
                        if (treeview.Rows.Count > 0)
                        {
                            frmExportExcel export = new frmExportExcel("DS_KhuVuc", treeview);
                            if (export.ShowDialog() == DialogResult.OK)
                            {
                                KQ = true;
                            }
                        }
                        break;
                    case EnumGUI.ThaoTac.timkiem:
                        SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> { });
                        break;
                }
            }
            catch { }
            if (KQ) LoadToTreeview(treeview, StaticVar.DanhSachKhoSP);
        }
        private void btn_Click(object sender, EventArgs e)
        {
            KQ = false;
            Button btn = (Button)sender;
            string thaotac = btn.Tag.ToString();

            switch (thaotac)
            {
                case "them":
                    XuLy(EnumGUI.ThaoTac.them);
                    break;
                case "sua":
                    XuLy(EnumGUI.ThaoTac.sua);
                    break;
                case "xoa":
                    XuLy(EnumGUI.ThaoTac.xoa);
                    break;
                case "xem":
                    XuLy(EnumGUI.ThaoTac.xem);
                    break;
                case "timkiem":
                    XuLy(EnumGUI.ThaoTac.timkiem);
                    break;
                case "nhapexcel":
                    XuLy(EnumGUI.ThaoTac.nhapexcel);
                    break;
                case "xuatexcel":
                    XuLy(EnumGUI.ThaoTac.xuatexcel);
                    break;
            }
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == txtTimKiem.Name)
                UIHelper.txt_KeyPress(sender, btnTimKiem, e);
        }

        private void treeview_DoubleClickElement(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            XuLy(EnumGUI.ThaoTac.sua);
        }
    }
}
